package com.codingfly.config;

import com.alibaba.nacos.api.naming.listener.Event;
import com.alibaba.nacos.api.naming.listener.EventListener;
import com.alibaba.nacos.api.naming.listener.NamingEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.context.named.NamedContextFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class ServiceEventListener implements EventListener {
    private static final Logger logger = LoggerFactory.getLogger(ServiceEventListener.class);
    @Resource
    private NamedContextFactory factory;

    /** 监听服务的变更 并销毁原有的 服务列表 */
    @Override
    public void onEvent(Event event) {
        if (event instanceof NamingEvent) {

            logger.error("========>>>>>服务下线<<<<==========");
            //销毁服务上下文
            factory.destroy();
        }
    }
}